package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void save(ShoppingCartDTO shoppingCartDTO) {
        // 判断当前假如到购物车中的商品是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        //属性拷贝
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //获取当前用户
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        // 如果商品已经存在 ，那么数量就要加一
        if (list != null && list.size() > 0) {
            ShoppingCart cart = list.get(0);
            //将其商品数量加一
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
        } else {
            //判断当前购物车中是菜品的数据还是套餐的数据

            //获取菜品数据
            Long dishId = shoppingCart.getDishId();
            if (dishId != null) {
                //获取菜品数据
                Dish dish = new Dish();
                DishVO dishVO = dishMapper.selectDishById(dishId);
                BeanUtils.copyProperties(dishVO, dish);
                //设置菜品的数据
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

            } else {
                //获取套餐数据
                Setmeal setmeal = new Setmeal();
                SetmealVO setmealVO = setmealMapper.selectById(shoppingCart.getSetmealId());
                //测试获取套餐的id
                System.out.println("测试获取套餐的id------------------------------------------"+setmealVO.getId());

                BeanUtils.copyProperties(setmealVO, setmeal);
                //设置套餐的数据
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }

            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 如果该商品不存在 就添加到数据库中
            shoppingCartMapper.insert(shoppingCart);
        }


    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> GetListShopCart() {
        return shoppingCartMapper.GetListShopCart(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车中的一个商品
     *
     * @param shoppingCartDTO
     */
    @Override
    public void rmShoppingCartById(ShoppingCartDTO shoppingCartDTO) {
        //判断是否是菜品 如果是就根据id删除
        ShoppingCart shoppingCart = new ShoppingCart();
        //属性拷贝
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        if (list != null && list.size() > 0) {
            shoppingCart = list.get(0);
            //判断当前是菜品还是套餐
            if (shoppingCart.getDishId() != null && shoppingCart.getSetmealId() == null) {
                //如果购物车中的商品数量是1 ，那就直接删除
                if (shoppingCart.getNumber() == 1) {
                    shoppingCartMapper.deleteById(shoppingCart.getId());
                }
                else {
                    //如果数量不是1数量减1
                    shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                    shoppingCartMapper.updateNumberById(shoppingCart);
                }
            }
            else {
                //获取套餐数据
                Setmeal setmeal = new Setmeal();
                SetmealVO setmealVO = setmealMapper.selectById(shoppingCart.getSetmealId());
                BeanUtils.copyProperties(setmealVO, setmeal);
                //如果购物车中的商品数量是1 ，那就直接删除
                if (shoppingCart.getNumber() == 1) {
                    shoppingCartMapper.deleteById(shoppingCart.getId());
                } else {
                    //如果不是为1 那就只减除数量
                    shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                    shoppingCartMapper.updateNumberById(shoppingCart);
                }
            }
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        //清除当前用户购物车中的所有商品
        shoppingCartMapper.cleanShoppingCart(BaseContext.getCurrentId());
    }
}
